package com.goldman.oss.common.utils;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.UUID;


public class FileUtils {

    public static String generateFileName(MultipartFile file) {
        return UUID.randomUUID().toString().replace("-", "") + getFileExt(file);
    }

    public static String generateFileName(File file) {
        return UUID.randomUUID().toString().replace("-", "") + getFileExt(file);
    }

    public static String getFileExt(MultipartFile file) {
        return getExt(file.getOriginalFilename());
    }

    public static String getFileExt(File file) {
        return getExt(file.getName());
    }

    private static String getExt(String originnalFileName) {
        String ext = "";
        if (StringUtils.isNotBlank(originnalFileName) &&
                originnalFileName.lastIndexOf(".") >= 0) {
            ext = originnalFileName.substring(originnalFileName.lastIndexOf("."));
        }
        return ext;
    }

}
